﻿namespace AsyncLog
{
	using System;
	using System.Text;
	using System.Windows.Forms;

	using PugLib.Math;

	public sealed class AsyncLogTestDriver : IDisposable
	{
		private readonly AsyncLogDemo _destination;
		private readonly Timer _timer = new Timer();

		public AsyncLogTestDriver(AsyncLogDemo destination)
		{
			_destination = destination;
		}

		#region IDisposable Members

		public void Dispose()
		{
			if (_timer != null)
			{
				_timer.Tick -= TimerTick;
				_timer.Dispose();
			}
			GC.SuppressFinalize(this);
		}

		#endregion

		internal void Start(int interval)
		{
			_timer.Interval = interval;
			_timer.Tick += TimerTick;
			_timer.Start();
		}

		internal void Stop()
		{
			_timer.Stop();
		}

		private void TimerTick(Object sender, EventArgs e)
		{
			String message = CreateRandomMessage();
			_destination.Controller.AddMessage(message);
		}

		public static String CreateRandomMessage()
		{
			int length = PugRandom.GetRandomInRange(10, 256);
			StringBuilder entry = new StringBuilder();
			for (int i = 0; i < length; i++)
			{
				char random = (char)PugRandom.GetRandomInRange(48, 126);
				entry.Append(random);
			}
			return entry.ToString();
		}
	}
}